<template>
	<view>
		<view style="margin: 10rpx;">
			<form @submit="formSubmit" @reset="formReset">
				<view style="height: 100rpx;margin-top: 30rpx;">
					<textarea @blur="bindTextAreaBlur" auto-height placeholder="输入标题" maxlength="36" name="title" v-model="title"/>
				</view>
				<hr>
				<view style="min-height: 100rpx;width: 100%;">
					<textarea @blur="bindTextAreaBlur" auto-height placeholder="输入内容" maxlength="-1" style="width: 100%;" name="desc" v-model="desc"/>
				</view>
				<view style="margin: 30rpx;">
					<uni-file-picker limit="1" auto-upload="true" @select="uploadFile" v-model="selectImage"></uni-file-picker>
				</view>
				<view>
					<view style="padding: 10rpx;border: solid #999 1px;margin-bottom: 10rpx;font-size: 28rpx;" @click="selectBook()"> {{selectBookText}}<text style="float: right;">></text></view>
				</view>
				<view style="margin-bottom: 30rpx;">
					<uni-data-select name="type"
					  empty-text="请选择交流贴分类" v-model="selectCategory" 
					  :localdata="range"
					  placeholder="请选择交流贴分类"
					  @change="change"
					></uni-data-select>
				</view>
				<button type="default" plain="true" form-type="submit" >确认</button>
			</form>
			<view style="margin:30rpx;font-size: 30rpx;color:#999"><navigator url="/pages/add/addBook">想申请一本书?</navigator></view>
			
		</view>
		
	</view>
</template>

<script setup>
import { ref } from 'vue';
import {baseUrl} from '../../static/js/util.js'
import {onLoad,onShow} from "@dcloudio/uni-app"
	let range=ref([])
	let isSubmit=0
	let selectBookText=ref('请选择一本书籍')
	let selectBookId=0;
	let selectCategory=ref(0)
	let userInfo={}
	let imageId=''
	let selectImage=ref([])
	let title=ref("")
	let desc=ref("")
	let id=0
	
	uni.request({
		url:baseUrl+"/category/list",
		method:'GET',
		success:res=>{
			res.data.data.forEach((item)=>{
				let a={value:item.id,text:item.name}
				range.value.push(a)
			})
		}
	})
	function formSubmit(e){
		if(selectBookId===0){
			uni.showToast({
				icon:'fail',
				title:'请先选择一本书'
			})
			return 
		}
		if(selectCategory.value===0){
			uni.showToast({
				icon:'fail',
				title:'请先选择分类'
			})
			return 
		}
		e.detail.value.bookId=selectBookId
		e.detail.value.userId=JSON.parse(userInfo).id
		e.detail.value.imageId=imageId
		e.detail.value.categoryId=selectCategory.value
		e.detail.value.likeCount=0
		e.detail.value.commentCount=0
		if(id!=0){
			e.detail.value.id=id
			e.detail.value.likeCount=''
			e.detail.value.commentCount=''
		}
		
		let Authorization=localStorage.getItem("Authorization")
		console.log(Authorization)
		uni.request({
			url:baseUrl+"/post/saveOrUpdate",
			method:'POST',
			data:e.detail.value,
			header:{'Authorization':Authorization},
			success:res=>{
				uni.showToast({
					icon:res.data.code==200?'success':'error',
					title:res.data.msg
				})
				if(res.data.code==200){
					uni.navigateTo({
						url:'/pages/cards/cards?userId='+e.detail.value.userId
					})
				}
			}
		})
	}
	function uploadFile(e){
		let Authorization=localStorage.getItem("Authorization")
		// console.log(e)
		uni.uploadFile({
			url:baseUrl+"/file/uploadImage",
			method:'POST',
			name:"file",
			header:{'Authorization':Authorization},
			filePath:e.tempFilePaths[0],
			success:res=>{
				imageId=JSON.parse(res.data).data
			},
			fail(f) {
				console.log(f)
			}
		})
		
	}
	function change(){
		console.log(selectCategory.value)
	}
	
	function selectBook(){
		uni.navigateTo({
			url:'/pages/add/selectBook'
		})
	}
	
	
	
	onShow(()=>{
		let user=localStorage.getItem("user")
		if(user==null){
			uni.showToast({
				icon:'error',
				title:'请先登录'
			})
			setTimeout(() => {
				uni.switchTab({
					url: "/pages/home/home"
				})
			}, 500)
		}
		else{
			if(sessionStorage.getItem("selectBookId")!=null && sessionStorage.getItem("selectBookName")!=null){
				selectBookId=sessionStorage.getItem("selectBookId")
				selectBookText.value=sessionStorage.getItem("selectBookName")
				sessionStorage.removeItem("selectBookId")
				sessionStorage.removeItem("selectBookName")
			}
			
			userInfo=user;
		}
	})
	
	onLoad(option =>{
		let user=localStorage.getItem("user")
		if(user==null){
			uni.showToast({
				icon:'error',
				title:'请先登录'
			})
			setTimeout(() => {
				uni.switchTab({
					url: "/pages/home/home"
				})
			}, 500)
			return;
		}
		if (option.postId != null) {
			id = option.postId
			requestDetail()
		}
	})
	
	function requestDetail(){
		let Authorization=localStorage.getItem("Authorization")
		let user = localStorage.getItem("user")
		let userId = JSON.parse(user).id
		uni.request({
			url: baseUrl + '/post/detail',
			header: {
				'Authorization': Authorization
			},
			data: {
				id,userId
			},
			success: (res) => {
				fillData(res.data)
			}
		})
	}
	
	function fillData(data) {
		title.value = data.title
		desc.value = data.desc
		selectImage.value=[{
			url: data.imagePath,
			extname: 'png',
			name: 'imageName.png'
		}]
		selectBookId=data.bookId
		selectBookText.value=data.bookName
		selectCategory.value=data.categoryId
		imageId=data.imageId
		// username.value = data.username
		// imagePath.value = data.imagePath;
		// profilePath.value = data.profilePath;
		// categoryName.value = data.categoryName
		// bookName.value = data.bookName;
		// likeCount.value = data.likeCount;
		// commentCount.value = data.commentCount;
		// isLike.value=data.isLike
	}
</script>

<style lang="scss" scoped>

</style>
